package com.bawei.module_my.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.bawei.module_base.net.bean.my.FindPrivateLetterListBean
import com.bawei.module_base.net.bean.wide.FindGiftRecordByUidBean
import com.bawei.module_base.net.util.HttpUtils
import com.bawei.module_base.net.util.HttpUtils2
import com.bawei.module_base.ui.base.BaseResult
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File


class MyViewModel :ViewModel() {
    val dataModifyHeadPic = MutableLiveData<BaseResult<String?>>()
    fun postModifyHeadPic(file: File) {
        val body = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), file)
        val part: MultipartBody.Part = MultipartBody.Part.createFormData("image", file.name, body)
        viewModelScope.launch(Dispatchers.IO) {
            var result = HttpUtils2.apiService.postModifyHeadPic(part)
            dataModifyHeadPic.postValue(result)
        }
    }

    //我的礼物
    val dataFindGiftRecordByUid = MutableLiveData<BaseResult<List<FindGiftRecordByUidBean>>>()
    fun getFindGiftRecordByUid( type: Int, page: Int, size: Int){
        viewModelScope.launch(Dispatchers.IO) {
            var result = HttpUtils.apiService.getFindGiftRecordByUid(840,
                "1717402826096840",
                type, page, size)
            dataFindGiftRecordByUid.postValue(result)
        }
    }
    val dataFindPrivateLetterListBean= MutableLiveData<BaseResult<List<FindPrivateLetterListBean>>>()
    fun getFindPrivateLetterList(){
        viewModelScope.launch(Dispatchers.IO) {
            var result = HttpUtils2.apiService.getFindPrivateLetterList()
            dataFindPrivateLetterListBean.postValue(result)
        }
    }
}